package cn.ffcs.security.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.web.BaseAction;
import cn.ffcs.system.dict.model.Dict;
import cn.ffcs.system.dict.service.DictService;
import cn.ffcs.security.model.User;
import cn.ffcs.security.service.IUserService;

/**
 * 
 *  用户 action<br/> <table>
 * <tr>
 * <td>负责人/principal: </td>
 * <td colspan="2">liuzhh</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author liuzhh
 * @version $Id$
 */
public class UserAction<U extends User> extends BaseAction<U>
{
    /** 网络标识 */
    private static final long serialVersionUID = -4686298088541775911L;
    /** 业务POJO **/
    private U user;
    /** 业务列表数据 **/
    private List<U> users;
    /** 数据字典服务 **/
    private DictService dictService;
    /** 有效性字典数据 **/
    private List<Dict> lValid;
    
    /**
     * 保存数据操作，负责新增和修改
     */
    public String saveOrUpdate()
    {
        baseService.saveOrUpdate(user);
        Criterion cond = Restrictions.eq("dictType.id", 77);
        lValid=dictService.findBy(cond);
        return SUCCESS;
    }
    
    /**
     * 逻辑删除
     * @return
     */
    public String remove()
    {
        baseService.remove(user);
        return SUCCESS;
    }
    
    /**
     * 批量删除
     * @return
     */
    public String batchDel()
    {
        String[] ids = getContextObjects("ids");
        baseService.batchDel(ids);
        ;
        return SUCCESS;
    }
    
    /**
     * 根据ID查找
     * @return
     */
    public String findById()
    {
        if (user != null && user.getId() != 0)
        {
            user = (U) baseService.findById(user.getId());
        }
        Criterion cond = Restrictions.eq("dictType.id", 77);
        lValid=dictService.findBy(cond);
        
        return SUCCESS;
    }
    
    /**
     * 
     * 组合查询条件
     * @return
     */
    public Criterion combineCond(){
        Criterion cond = null;
        if (super.getConds() != null)
        {
            //遍历参数表
            Iterator i = super.getConds().entrySet().iterator();
            while (i.hasNext())
            {
                Entry entry = (Entry)i.next();
                String key=(String)entry.getKey();
                if (super.getConds().get(key) != null
                        && !StringUtils.isEmpty(super.getConds().get(key).toString()))
                {
                    //根据实际情况做处理
                    Criterion temp=null;
                    if (key.equalsIgnoreCase("userID") ){
                        temp=Restrictions.like(key, "'%"+super.getConds().get(key).toString()+"%'");
                    }else if(key.equalsIgnoreCase("person")){
                    	temp=Restrictions.like("person.name", "'%"+super.getConds().get(key).toString()+"%'");
                    }else{
                        temp=Restrictions.eq(key, super.getConds().get(key));                        
                    }
                    if (cond == null)
                    {
                        cond = temp;
                    }
                    else
                    {
                        cond = Restrictions.and(cond, temp);
                    }
                }
            }
        }
        return cond;
    }    
    
    /**
     * 获取业务POJO
     * @return
     */
    public U getUser()
    {
        return user;
    }

    /**
     * 设置业务POJO
     * @param user
     */
    public void setUser(U user)
    {
        this.user = user;
    }
    
    /**
     * 获取用户列表数据
     * @return
     */
    public List<U> getUsers()
    {
        return users;
    }
    
    /**
     * 设置业务用户列表
     * @param users
     */
    public void setUsers(List<U> users)
    {
        this.users = users;
    }

    /**
     * 获取数据字典服务
     * @return the dictService
     */
    public DictService getDictService()
    {
        return dictService;
    }

    /**
     * 设置数据字典服务
     * @param dictService 
     */
    public void setDictService(DictService dictService)
    {
        this.dictService = dictService;
    }

    /**
     * 获取有效性字典数据列表
     * @return the lValid
     */
    public List<Dict> getLValid()
    {
        return lValid;
    }

    /**
     * 设置有效性字典数据列表
     * @param valid the lValid to set
     */
    public void setLValid(List<Dict> valid)
    {
        lValid = valid;
    }

}
